import 'package:flutter/material.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

class DateTimePickerPage extends StatefulWidget {
  const DateTimePickerPage({Key? key}) : super(key: key);

  @override
  _DateTimePickerPageState createState() => _DateTimePickerPageState();
}

class _DateTimePickerPageState extends State<DateTimePickerPage> {
  DateTime _nowDate=DateTime.now();
  DateTime _nowDateTime=DateTime.now();

  void _showDatePicker(){
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1988, 3, 5),
        maxTime: DateTime(2099, 6, 7),
        onChanged: (date) {
          print('change $date');
        },
        onConfirm: (date) {
          print('confirm $date');
          setState(() {
            this._nowDate=date;
          });
        },
        currentTime: _nowDate,
        locale: LocaleType.zh,
    );
  }

  void _showDateTimePicker(){
    DatePicker.showDateTimePicker(context,
      showTitleActions: true,
      minTime: DateTime(1900, 5, 5, 20, 50),
      maxTime: DateTime(2099, 6, 7, 05, 09),
      onChanged: (date) {
        print('change $date in time zone ' + date.timeZoneOffset.inHours.toString());
      },
      onConfirm: (date) {
        print('confirm $date');
        setState(() {
          this._nowDateTime=date;
        });
      },
      locale: LocaleType.zh);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DateTimePicker'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          InkWell(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("${formatDate(_nowDate, [yyyy, '年', mm, '月', dd ,'日'])}"),
                Icon(Icons.arrow_drop_down),
              ],
            ),
            onTap: _showDatePicker
          ),
          SizedBox(height: 20),
          InkWell(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text("${formatDate(_nowDateTime, [yyyy, '年', mm, '月', dd ,'日','',HH, ':', nn, ':', ss])}"),
                  Icon(Icons.arrow_drop_down),
                ],
              ),
              onTap: _showDateTimePicker
          ),
        ],
      ),
    );
  }
}
